package com.unicom.wostore.dsp.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class RedisApi {

	@Autowired
	RedisTemplate redisTemplate;

	public boolean set(String key, String value) {
		redisTemplate.getConnectionFactory();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new StringRedisSerializer());
		ValueOperations<String, String> valueOper = redisTemplate.opsForValue();

		try {
			valueOper.set(key, value);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean setObj(String key, Object value) {
		redisTemplate.getConnectionFactory();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		ValueOperations<String, Object> valueOper = redisTemplate.opsForValue();
		try {
			valueOper.set(key, value);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * set key and value to redis
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean setForTimeout(String key, String value, int expireTime) {
		redisTemplate.getConnectionFactory();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new StringRedisSerializer());
		ValueOperations<String, String> valueOper = redisTemplate.opsForValue();
		try {
			valueOper.set(key, value);
			redisTemplate.expire(key, (long) expireTime, TimeUnit.MINUTES);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean setForTimeout(String key, Object value, int expireTime) {
		redisTemplate.getConnectionFactory();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
		ValueOperations<String,Object> valueOper = redisTemplate.opsForValue();
		try {
			if (value!=null){
				valueOper.set(key, value);
			}

			redisTemplate.expire(key, (long) expireTime, TimeUnit.MINUTES);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	public boolean setForTimeout(String key, int expireTime) {
		try {

			redisTemplate.expire(key, (long) expireTime, TimeUnit.MINUTES);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}


	/**
	 * 判断某个key是否存在
	 * 
	 * @param key
	 * @return
	 */
	public boolean exist(String key) {
		redisTemplate.getConnectionFactory();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new StringRedisSerializer());
		ValueOperations<String, String> valueOper = redisTemplate.opsForValue();

		try {
			return redisTemplate.hasKey(key);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 获取数据
	 * 
	 * @param key
	 * @return
	 */
	public String get(String key) {
		String value = null;
		redisTemplate.getConnectionFactory();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new StringRedisSerializer());
		ValueOperations<String, String> valueOper = redisTemplate.opsForValue();
		try {
			value = valueOper.get(key);
		} catch (Exception e) {
			// 释放redis对象
			e.printStackTrace();
		}

		return value;
	}

	public Object getObj(String key) {

		Object value = null;
		redisTemplate.getConnectionFactory();
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
		ValueOperations<String, Object> valueOper = redisTemplate.opsForValue();
		try {
			value = valueOper.get(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 返还到连接池
		}

		return value;

	}

	/**
	 * 从队列顶部增加元素
	 * 
	 * @param key
	 * @return
	 */
	public boolean lpush(String key, String value) {

		return false;
	}

	/**
	 * 从队列顶部弹出元素
	 * 
	 * @param key
	 * @return
	 */
	public String lpop(String key) {
		String value = null;

		return value;
	}

	/**
	 * 从队列底部增加元素
	 * 
	 * @param key
	 * @return
	 */
	public boolean rpush(String key, String value) {

		return false;
	}

	/**
	 * 从列表底部弹出元素
	 * 
	 * @param key
	 * @return
	 */
	public String rpop(String key) {
		String value = null;
		return value;
	}

	/**
	 * 监视一个KEY元素
	 * 
	 * @param
	 * @return
	 */
	public String watch(String... keys) {
		String value = null;

		return value;
	}

	/**
	 * 获取队列长度
	 * 
	 * @param key
	 * @return
	 */
	public Long llen(String key) {
		Long value = null;

		return value;
	}

	public boolean del(String key) {
		redisTemplate.getConnectionFactory();
		redisTemplate.setKeySerializer(new StringRedisSerializer());

		try {
			redisTemplate.delete(key);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public List<String> lrange(String key) {
		List<String> value = null;

		return value;
	}
}
